package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.CommonProblemAddDto;
import com.dog.basics.bean.dto.admin.common.CommonProblemEditDto;
import com.dog.basics.bean.dto.admin.common.CommonProblemSelectDto;
import com.dog.basics.bean.model.common.SysCommonProblem;
import com.dog.basics.bean.model.common.SysCommonProblemClass;
import com.dog.basics.bean.vo.admin.sys.CommonProblemDetailVo;
import com.dog.basics.bean.vo.admin.sys.CommonProblemPageVo;
import com.dog.basics.bean.vo.api.common.CommonProblemAppPageVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.SysCommonProblemMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.ISysCommonProblemClassService;
import com.dog.basics.service.common.ISysCommonProblemService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 常见问题 服务实现类
 * </p>
 *
 * @author fengjukang
 * @since 2019-10-11
 */
@Service
@AllArgsConstructor
public class SysCommonProblemServiceImpl extends ServiceImpl<SysCommonProblemMapper, SysCommonProblem>
        implements ISysCommonProblemService {

    private final ISysCommonProblemClassService isyscommonproblemclassservice;

    /**
     * 常见问题列表
     * @param page
     * @param dto
     * @return
     * @author guonh
     */
    @Override
    public IPage<CommonProblemPageVo> getCommonProblemPage(PageParam page, CommonProblemSelectDto dto) {
        this.page(page, Wrappers.<SysCommonProblem>query().lambda()
                .like(ObjectUtils.isNotNull(dto.getTitle()), SysCommonProblem::getTitle,
                        dto.getTitle()));
        List<SysCommonProblem> records = page.getRecords();
        page.setRecords(records.stream().map(rec -> {
            CommonProblemPageVo commonProblemPageVo = BeanUtil.toBean(rec, CommonProblemPageVo.class);
            //查询分类名称
            SysCommonProblemClass sysCommonProblemClass = isyscommonproblemclassservice.getById(commonProblemPageVo.getProblemClassId());
            commonProblemPageVo.setProblemClassName(ObjectUtils.isNotNull(sysCommonProblemClass) ? sysCommonProblemClass.getTitle() : null);
            return commonProblemPageVo;
        }).collect(Collectors.toList()));
        return page;
    }

    /**
     * 常见问题添加
     * @param dto
     * @return
     * @author guonh
     */
    @Override
    public Boolean addCommonProblem(CommonProblemAddDto dto) {
        SysCommonProblemClass byId = isyscommonproblemclassservice.getById(dto.getProblemClassId());
        Validator.validateNotNull(byId, "分类不存在");
/*        SysCommonProblem sysCommonProblem = BeanUtil.copyProperties(dto, SysCommonProblem.class);
        sysCommonProblem.setCreateBy(SecurityUtils.getUser().getUserId());
        sysCommonProblem.setCreateTime(DateUtil.currentSeconds());
        Validator.validateTrue(this.save(sysCommonProblem), "常见问题添加失败");*/
        return Boolean.TRUE;
    }

    /**
     * 常见问题修改
     *
     * @param dto
     * @return
     * @author guonh
     */
    @Override
    public Boolean editCommonProblem(CommonProblemEditDto dto) {
        Validator.validateNotNull(this.getById(dto.getId()), "无此信息");
        SysCommonProblem sysCommonProblem = new SysCommonProblem();
        BeanUtil.copyProperties(dto, sysCommonProblem);
        sysCommonProblem.setUpdateBy(SecurityUtils.getUser().getUserId());
        sysCommonProblem.setUpdateTime(DateUtil.currentSeconds());
        Validator.validateTrue(updateById(sysCommonProblem), "常见问题修改失败");
        return Boolean.TRUE;
    }

    /**
     * 常见问题删除
     *
     * @param id
     * @return
     * @author guonh
     */
    @Override
    public Boolean delCommonProblem(Long id) {
        SysCommonProblem byId = this.getById(id);
        Validator.validateNotNull(byId, "无此信息");
        Validator.validateTrue(removeById(id), "常见问题删除失败");
        return Boolean.TRUE;
    }

    /**
     * 常见问题列表
     *
     * @param page
     * @param dto
     * @return
     * @author guonh
     */
    @Override
    public IPage<CommonProblemAppPageVo> getCommonProblemPageList(PageParam page, CommonProblemSelectDto dto) {
        this.page(page, Wrappers.<SysCommonProblem>query().lambda()
                .eq(SysCommonProblem::getProblemClassId, dto.getClassId())
                .eq(SysCommonProblem::getProblemType, dto.getProblemType())
                .like(ObjectUtils.isNotNull(dto.getTitle()), SysCommonProblem::getTitle, dto.getTitle()));
        List<SysCommonProblem> record = page.getRecords();
        page.setRecords(record.stream().map(sysCommonProblem -> BeanUtil.toBean(sysCommonProblem, CommonProblemAppPageVo.class)).collect(Collectors.toList()));
        return page;
    }

    /**
     * 常见问题详情
     *
     * @param id
     * @return
     * @author guonh
     */
    @Override
    public CommonProblemDetailVo getCommonProblemDetail(Long id) {
        SysCommonProblem byId = this.getById(id);
        Validator.validateNotNull(byId, "无此信息");
        return null;

    }
}
